/**
* Created by 枝枝_zhizhi on 2017/7/6.
*/
/*
 * 常用方法封装
 */
var app = getApp();
/**
 * 发起http请求
 * @param obj  请求的参数(object) url(string),data(object),method(string),success(function),header(object)
 */
function http (obj) {
    var that = this;
    let data = obj.data ||{};
    data.from = 'wxapp';
    wx.request({
        url: app.globalData.commonURL + obj.url,
        data: data,
        method: obj.method || 'GET',
        success: function (res) {
            obj.success && obj.success(res);
        },
        header: obj.header || {
            // 'content-type': 'application/json'
            'content-type': 'application/x-www-form-urlencoded'
        },
        fail: function (erorr) {
            obj.fail && obj.fail(erorr);
            ////////console.log(erorr);
        }
    })
}


/**
 * 弹窗
 */

    /**
     * toast 弹窗
     * @param content 内容(string)
     * @param title 标题(string)
     * @param icon 图标(string('success' || 'loading'))
     * @param duration
     */
    function toast (content, title, icon, duration) {
        wx.showToast({
            content: content,
            title: title || '提示',
            icon: icon || 'loading',
            duration: duration || 1500
        })
    }

    /**
     * modal 弹窗
     * @param title 标题(string)
     * @param content  内容(string)
     * @param success  成功的回调函数(function)
     */
    function modal (title, content, success) {
        wx.showModal({
            title: title,
            content: content,
            success: success || function () {
                
            }
        })
    }
//操作购物车数据    参数规则 specif=是否是属性商品的操作
 function operating (e,thats,specif) {
     var that=thats;
     //////console.log(that);
     if(that.data.isSplice){
         that.setData({
             isSplice: false
         });
     }
     //列表页的减少商品
     if(e.currentTarget.dataset.icon==1){
         ////////console.log('列表页的减少商品');
         let pid=e.currentTarget.dataset.pid;
         let cartData=that.data.cartDataList;
       //在购物车中找到商品列表的下标
         cartData[0].buydata.forEach(function(value,index,array){
             ////////console.log(value.goodsid);
             if(value.goodsid==pid){
                 ////////console.log(e.currentTarget.dataset.index);
                 setAge(1, 0, index,that,e.currentTarget.dataset.index);
                 //symbol, shop, p,thats,isIcon
             }
         })
     }else{
         var shop = e.currentTarget.dataset.shopid;
         var p=0;
         //如果商品带有属性 改变pid的获得方式
         if(specif){
              p = specif;
         }else{
             ////////console.log('非商品属性的操作');
              p = e.currentTarget.dataset.pid;
              ////////console.log(p);
         }
         if (e.target.id == "add") {
             ////////console.log("add");
             specif?setAge(1, 0, p,thats,undefined,true):setAge(1, shop, p,thats);
             //setAge(1, shop, p,thats)
         } else if (e.target.id == "less") {
             specif?setAge(2, 0, p,thats,undefined,true):setAge(2, shop, p,thats);
             ////////console.log("less");
            //setAge(2, shop, p,thats)
         }
     }
         if(that.getLunchBox){
             // that.setData({
             //    valueIsSet:true
             // });
             that.getLunchBox('utils');
         }
}
//数量操作1  //参数规则   symbol=增加还是减少 1=add 2=less ,shop=店铺who,p=商品id,thats=this,isIcon=是否是商品列表的减少商品
 function setAge (symbol, shop, p,thats,isIcon,isAttribute) {
  //////console.log('数量操作1------------------------------------setAge');
    var that = thats;
    var setData = that.data.cartDataList;//购物车数据
    let attrObj;
    let isLastNum=false;
     if(isAttribute){
         //console.log('属性商品的操作-------对数据-------------');
         let obj=getPDataIndex(0,p,setData);
        ////console.log('返回成功----------------------------------------------');
         ////console.log(obj);
         attrObj=obj;
         p=obj.p;
         // //console.log('总价');
         //
         // //console.log()

         ////////console.log(symbol);
     }else if(setData[shop]==undefined||setData[shop].buydata[p]==undefined||setData[shop].buydata[p]==[]){
        //////console.log('开始判断不通过');
         return}
          ////////console.log(attrObj);
    var pNum = setData[shop].buydata[p].buyNum;
    let pNumAttr=isAttribute?setData[0].buydata[p].buy_property[attrObj.pArrt].buyNum:'';
    // ////////console.log('当前个数');
    // ////////console.log(pNum);
    //buyNum
    if (symbol == 1) {
        let pLsit=that.data.details;
        ////console.log('减少数量');
        pNum--;
        pNumAttr--;
        if (isAttribute&&pNumAttr<=0){
            ////console.log('属性商品的最后一条了');
            ////console.log(attrObj);
            //setData[shop].buydata.splice(p, 1);
            setData[shop].buydata[p].buy_property.splice(attrObj.pArrt, 1);
            isLastNum=true;
        }
        if (pNum <= 0){
            ////console.log('该类商品的最后一条了--------------------------------------------------------');
            ////////console.log(setData[shop].buydata[p]);
            ////////console.log(p);
            if(that.data.details){
                if(isIcon!=undefined){//是否是商品列表的减少商品
                    pLsit[isIcon].cartNum=null;
                    that.setData({
                        details:pLsit,
                    })

                }else{
                    ////////console.log('不是列表的减少商品');
                    if(isAttribute){//属性商品的减少商品

                    }else{
                        ////////console.log('操作列表的减号');
                        let pid=setData[shop].buydata[p].goodsid;
                        ////////console.log(pid);
                        ////////console.log(pLsit);
                        pLsit.forEach(function(value,index,array){
                            if(pid==value.id){
                                ////////console.log('找到');
                                ////////console.log(index);
                                pLsit[index].cartNum=null;
                                that.setData({
                                    details:pLsit
                                })
                            }
                        });
                        ////////console.log('操作列表的减号结束-------------------------');
                        ////////console.log(that.data.details);
                    }


                }
            }

            setData[shop].buydata.splice(p, 1);
            //如果该商铺的购物车为空 删除该商铺的记录
            if (setData[shop].buydata.length == 0) {
                setData.splice(shop, 1);
                if(that.dataPairing){
                    that.dataPairing();
                }
                //如果购物车没有任何商品 清除购物车记录
                if (setData.length == 0) {
                    wx.removeStorage({
                        key: 'cartData',
                        success: function (res) {
                            that.setData({
                                cartData: [],
                                dataIsNull: true
                            });
                        }
                    });
                    that.setData({
                        cartDataList:[],
                        cartIsHidden:true,
                        ejectIshow:false
                    });
                    ////console.log('检查3');
                    ////console.log(that);
                    getNumPrice([],that);
                    return;
                }
            }

            setAges(setData,thats);
            return;
        }

        }
      else if (symbol == 2) {
        pNum++;
        pNumAttr++;
       // setAges(setData,thats);
    }
    setData[shop].buydata[p].buyNum = pNum;
    if(isAttribute){
        if(setData[0].buydata[p].buy_property[attrObj.pArrt]&&!isLastNum){
            ////console.log('设置属性商品的数量');
            ////console.log(pNumAttr);
            let attrP=setData[0].buydata[p].buy_property[attrObj.pArrt]
            attrP.buyNum = pNumAttr;
            // attrP.price=(attrP.unitPrice)*(attrP.buyNum);
             attrP.price= round2( attrP.price);
        }
    }
    ////console.log('最后的数据----------------');
     ////console.log(setData);
     //更新视图数据
     that.setData({
        cartDataList:setData
     });
     ////////console.log('最后修改完成的数据');
     ////////console.log(setData);
    setAges(setData,thats);
}
//数量操作2
 function setAges(setData,thats) {
    //修改缓存
   thats.setData({
        cartData: setData,
       cartDataList:setData
    });
    //转json
    var toStr = JSON.stringify(setData);
    wx.setStorage({
        key: "cartData",
        data: toStr
    });
     ////console.log('检查4');
     ////console.log(thats);
    getNumPrice(setData,thats);
}
//取出购物车数量和总价和操作列表数据f
function getNumPrice (setData,thats) {
        //console.log(setData);
        //console.log(thats);
    var cartNum = 0;
    var allPrice = 0;
    if(setData.length){
        for (var i = 0; i < setData.length; i++) {
            for (var s = 0; s < setData[i].buydata.length; s++) {
                cartNum += setData[i].buydata[s].buyNum;
                //console.log('数量价格');
                if(setData[i].buydata[s].buy_property.length>0){
                    //console.log('购物车属性商品的价格');
                    setData[i].buydata[s].buy_property.forEach(function(value,index,arr){
                        allPrice+=parseFloat(value.price)*parseFloat(value.buyNum);
                        //console.log(allPrice);
                    })
                }else{
                    //console.log('非属性商品的价格');
                    ////console.log(setData[i].buydata[s].buyNum);
                    allPrice += parseFloat(setData[i].buydata[s].buyNum) *parseFloat(setData[i].buydata[s].price);
                    ////console.log(setData[i].buydata[s].buyNum,setData[i].buydata[s].price)
                }
                //console.log('暂时的总价格');
                //console.log(allPrice=parseFloat(allPrice));

            }
        }
    }

    //console.log('得到价格总价格');
    allPrice=parseFloat(allPrice);
    //console.log(allPrice);
    thats.setData({
        cartAllNum:cartNum,
        allPrice:allPrice.toFixed(2),
        cartDataList:setData
    });
    if(thats.dataPairing){
        thats.dataPairing();
    }
    console.log('是否有配送费方法');
    if(thats.setShowPrice && thats.data.psOpen=='true'){
        thats.setShowPrice()
    }
}


/**
 * 打电话
 * @param num  电话号码(string)
 */
function onCall  (num) {
    wx.makePhoneCall({
        phoneNumber: num
    })
}



/**
 * 设置导航栏标题
 * @param title 标题名称
 */
 function setNavigationBarTitle (title) {
    wx.showNavigationBarLoading();
    wx.setNavigationBarTitle({
        title: title || ''
    });
    wx.hideNavigationBarLoading()
}


/**
 * 日期函数
 * @param day  相对于今天的哪一天（昨天传-1，今天传0，明天传1）
 * @returns {string}
 */
function getDay (day) {
    var today = new Date();

    var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;

    today.setTime(targetday_milliseconds); //注意，这行是关键代码

    var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    var tDate = today.getDate();
    tMonth = this.doHandleMonth(tMonth + 1);
    tDate = this.doHandleMonth(tDate);
    return tYear + "-" + tMonth + "-" + tDate;
}

function doHandleMonth (month) {
    var m = month;
    if (month.toString().length == 1) {
        m = "0" + month;
    }
    return m;
}


/**
 *
 * @param obj  请求的参数
 * obj: {
 *      who: 商户号，number，可不传，默认读取app.globalData.who，
 *      openid: 微信身份openid，string, 可不传，默认读取app.globalData.openid,
 *      from: 小程序支付请求, string, 可不传，默认是'wxapp'
 *      orderid: 订单号, number, 新建付款不用传，订单支付传订单号
 *      amount: 支付金额, number，新建支付必传，订单支付不用传
 *      paytype: 支付方式, number, 微信支付不用传， （5：微信支付    其他更多的方式有： 1 余额支付  2支付宝支付  3 货到付款   4 上门自取  5微信支付  6 店内现付    7 微信二维码支付  8 微信扫码付   9 全支付 支付宝网页支付   10 全支付微信扫码     11 全支付支付宝扫码     12全支付微信付     13全码付微信支付（有分润）     14全袋收银（口袋收银）    15 微信支付 (中信银行)）
 *      orderfortypes: 订单所属类型, number, 代表在线收银台订单 不用传， （0普通订单  1打赏订单   5  代表在线收银台订单
9储值卡充值支付单 10共享充电宝 11拼车 司机发单乘客发单
12 跑腿 代取代收业务）
 *      navigateUrl: 支付成功后跳转的url
 *      userid: 用户id, 可不传，默认取app.globalData.userid
 *      callback: 支付成功后的回调函数，可不传
 *
 *
 */

//获取默认地址
function getAddress (thats){
        var that = thats;
    //////////console.log("utils getAddress");
    //////////console.log(app.globalData.address);
        if (app.globalData.address == null) {
            that.setData({
                noAddress: false,
                address:""
            });
            return;
        }
        that.setData({
            address: app.globalData.address,
            noAddress: true
        });

}

function setNavigationBarTitle (title) {
    wx.showNavigationBarLoading();
    wx.setNavigationBarTitle({
        title: title || ''
    });
    wx.hideNavigationBarLoading()
}


function pay (obj) {
    wx.request({
        url: app.globalData.commonURL +  '/xcxorder/createpay',
        method: 'POST',
        header: {
            'content-type': 'application/x-www-form-urlencoded'

        },
        data: {
            'who': obj.who || app.globalData.who,
            'openid': obj.openid ||  app.globalData.openid,
            'from': obj.from || 'wxapp',  //小程序支付请求
            'orderid': obj.orderid || '',
            'amount': obj.money * 100 || '',
            'paytype': obj.paytype || 5,
            'orderfortypes': obj.orderfortypes || 5,
            'userid': obj.userid || app.globalData.userid
        },
        success: function (res) {
            ////////console.log(res);
           ////////console.log("utils 对象转json");
            ////////console.log(res.data);
            ////////console.log(res.data.wx_js_pay_data);
            var wx_js_pay_data = JSON.parse(res.data.wx_js_pay_data);
            wx.requestPayment({
                'timeStamp': wx_js_pay_data.timeStamp,
                'nonceStr': wx_js_pay_data.nonceStr,
                'package': wx_js_pay_data.package,
                'signType': 'MD5',
                'paySign': wx_js_pay_data.paySign,
                'success': function (res) {
                    obj.callback || function () {};
                    wx.switchTab({
                        url: obj.navigateUrl
                    });
                    //清空购物车
                this.removeCart();
                },
                'fail': function (error) {
                }
            })
        }
    })
}
function removeCart(){
    wx.removeStorage({
        key: 'cartData',
        success: function () {
        }
    });
}
/**
 * 开启debug
 * @param e
 */
function debug (e) {
    ////////console.log(e);
    if (e.timeStamp > 5000) {
        wx.setEnableDebug({
            enableDebug: true,
            success: function () {
                ////////console.log('debug开启成功');
            },
            fail: function () {
                wx.showToast({
                    title: 'debug开启失败'
                })
            }
        })
    }
}

/**
 * 关闭debug
 * @param e
 */
function closeDebug (e) {
    ////////console.log(e);
    if (e.timeStamp > 5000) {
        wx.setEnableDebug({
            enableDebug: false,
            success: function () {
                ////////console.log('debug关闭成功');
            },
            fail: function () {
                wx.showToast({
                    title: 'debug关闭失败'
                })
            }
        })
    }
}
//获取购物车数据 参数规则  t_this=this,isCartPage=是否是脚部 isAttribute=是否是属性
function getCart(t_this,isCartPage,isAttribute){
    //////////////console.log("获取购物车数据");
    var that=t_this;
    wx.getStorage({
        key: 'cartData',
        success: function(res) {
            //////////////console.log("获取成功");
            //////////////console.log(res.data);
            that.setData({
                dataIsNull:false
            });
            var obj = JSON.parse(res.data);
            if(isCartPage){
                ////console.log('检查1');
                ////console.log(that);
                getNumPrice(obj,that);
                that.setData({
                    cartDataList:obj,
                    isCartData:true,
                    cartIsHidden:false
                });
            }else if(isAttribute){//属性商品数量查找
                let pNum=0;//购物车中该属性商品的数量
                let pIdY=isAttribute.pIdY;//该类商品的id
                let pId=isAttribute.pId;//属性商品的id
                obj[0].buydata.forEach(function(value,index,arr){
                    ////////console.log(value);
                    if(value.goodsid==pIdY){
                        value.buy_property.forEach(function(val,ind,Arr){
                           if(val.allid==pId){
                               ////////console.log('找到');
                               pNum=val.buyNum;
                           }
                        })
                    }
                });
                ////////console.log('查找结束');
                isAttribute.success(pNum);
                return;
            }else{
                console.log('只查数量');
                console.log(obj[0].buydata.length);
                that.setData({
                    cartNum:obj[0].buydata.length
                })
            }
            if(that.getLunchBox){
                console.log('拿到购物车数据');
                that.getLunchBox();
            }
        },
        fail:function(){
            if(isAttribute){
                return;
            }else{
                //console.log("没有本地数据");
                that.setData({
                    cartDataList:[],
                    isCartData:false,
                    dataIsNull:true
                });
                if(isCartPage){
                    that.setData({
                        cartDataList:[],
                        isCartData:false
                    });
                }else{
                    that.setData({
                        cartNum:0
                    })
                }
            }

        }
    });


}
function debug (e) {
    //////////console.log(e);
    if (e.timeStamp > 5000) {
        wx.setEnableDebug({
            enableDebug: true,
            success: function () {
                //////////console.log('debug开启成功');
            },
            fail: function () {
                wx.showToast({
                    title: 'debug开启失败'
                })
            }
        })
    }
}
function closeDebug (e) {
    //////////console.log(e);
    if (e.timeStamp > 5000) {
        wx.setEnableDebug({
            enableDebug: false,
            success: function () {
                //////////console.log('debug关闭成功');
            },
            fail: function () {
                wx.showToast({
                    title: 'debug关闭失败'
                })
            }
        })
    }
}
//加入购物车
function addCart (e,t_this,specif,vm) {
    ////console.log('utils 加入购物车');
    var that = t_this;
    //console.log('属性商品Id-------------------');
    //console.log(specif);
    var pData;
    var cartData=[];
    var StorageData;
    var  setPrice;
    //数据

    let buy_property=[];//包含 此商品的所有规格的数组
    if(specif){
          pData = that.data.specifData;//属性商品的商品数据

         var specifData= that;
         if(specif!=='noSel'){
             //////console.log('商品有规格属性');
             //拼接规格数据
             //拿到选中的规格
             ////////console.log('拿到选中的规格');
            let selectedData=vm.$data().selectedData;
             setPrice=vm.$data().setPrice;
            //逻辑太多加个给带属性的商品添加数据的函数
             //该方法只是 拼接当前规格商品的数据 添加当前规格商品
             buy_property.push(addCartM(pData,selectedData,specif,setPrice));
             ////////console.log('一级添加购物车方法');
         }

    }else{
        ////////console.log('非规格组件的添加商品');
        that.setData({
            detail:that.data.details[e.currentTarget.id]
        });
         pData = that.data.detail;
    }
    var cartDataPAll = {
        shopid: app.globalData.who,
        shopname: app.globalData.appData.shopName,
        buydata: [
            {
                price:pData.jg || 123456,
                goodsid: pData.id,
                buyNum: 1,
                title:pData.title,
                boxfee:pData.boxfee,
                discount:pData.discount,
                integral:pData.integral,
                pic:pData.pic,
                YN_full_buynum: 0,
                unit:pData.dw,
                buy_property:buy_property
            }
        ]
    };
    if(specif!=='noSel'&specif!=undefined){//此处bug!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //console.log('已有店铺添加属性 拿到设置价格');
        //console.log(setPrice);
        cartDataPAll.buydata[0].price=round2(setPrice);
    }
    ///////////////////////////////////////////////////
    //已有此商铺 生成的对象数据
    var cartDataP = {
        goodsid: pData.id,
        buyNum: 1,
        price: pData.jg,
        title: pData.title,
        boxfee:pData.boxfee,
        discount: pData.discount,
        integral: pData.integral,
        pic:pData.pic,
        YN_full_buynum: 0,
        unit:pData.dw,
        buy_property
    };
    if(specif!=='noSel'&specif!=undefined){//此处bug!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //console.log('属性加入购物车的价格');
        cartDataP.price=setPrice;
    }
    ////////console.log(cartDataPAll);
    cartData.push(cartDataPAll);
    ////////console.log('保存的商品的的数据格式----------------------------');
    ////////console.log(cartData);
    //先取出本地数据进行处理再覆盖
    wx.getStorage({
        key: 'cartData',
        success: function (res) {
            var pData;
            if(specif){
                pData= that.data.specifData
            }else{
                pData = that.data.detail;
            }
            var pagePData = pData;
            StorageData = res.data;
            //转js对象   eval('(' + StorageData + ')')
            var result = JSON.parse(StorageData);
            ////////console.log('购物车数据----------------------------');
            ////////console.log(result);
            //判断是否是重复商铺
            var isShop = true;
            for (var i = 0; i < result.length && isShop; i++) {
                //商铺号是
                if (result[i].shopid == app.globalData.who) {
                    isShop = false;
                    //判断是否是重复商品
                    var pData = result[i].buydata;

                    var isFro = true;//待验证 是否能及时停止循化
                    for (var s = 0; s < pData.length && isFro; s++) {
                        if (pData[s].goodsid == pagePData.id) {
                            pData[s].buyNum++;
                            ////////console.log(pData[s]);
                            isFro = false;
                            ////////console.log('重复商品');
                            ////////console.log('购物车此类商品不带属性的数据');
                            //如果要加入的数据是带属性的
                            if(buy_property.length!=0){
                                //遍历同一类商品的属性商品列表
                               //购物车内此商品已有属性数据
                                if(pData[s].buy_property.length>0){
                                    ////////console.log('购物车内此商品已有属性数据');
                                    ////////console.log('遍历同一类商品的属性商品列表');
                                    pData[s].buy_property.forEach(function(value,index,arr){
                                        if(value.allid==buy_property[0].allid){
                                            ////console.log('商品属性一样');
                                            value.buyNum++;
                                            ////console.log('初始价格');
                                        }else if(index>=pData[s].buy_property.length-1){
                                            ////console.log('最后一条了  没有相同属性的商品0-------------------');
                                            pData[s].price=round2(parseFloat(pData[s].price)+parseFloat(buy_property[0].price));
                                            ////console.log(pData[s].price);
                                            pData[s].buy_property.push(buy_property[0]);
                                            ////console.log('最后的数据');
                                            ////console.log( pData[s].buy_property);
                                        }
                                    })
                                }else{
                                    pData[s].buy_property.push(buy_property[0]);
                                }

                            }
                        }
                    }
                    if (isFro) {
                        pData.push(cartDataP);
                        setwxage(result,that);
                        return;
                    }
                    result.buydata = pData;
                    setwxage(result,that);
                    return;
                }
            }
            if(isShop){
                ////console.log('存储3');
                result.push(cartDataPAll);
               setwxage(result,that);

            }

        },
        fail: function (err) {
            setwxage(cartData,that);

        }
    });
}
//给带属性的商品添加数据的函数 参数规则 pData=商品信息  selectedData = 选中项的信息 specif=拼接好的商品属性id setPrice=算好的价格
function addCartM(pData,selectedData,specif,setPrice){
    //console.log('addCart  ---');
    //console.log(pData);
    let unitPrice;
    let buy_property;
    let sel=selectedData;//规格设置的数据信息
    let property=[];//规格信息数组
    let alltexts='';//规格总描述
    sel.forEach(function(value,index,arr){
       // ////////console.log(value);
        alltexts+=pData.property[index].name+':';
       value.indexs.forEach(function(val,ind,arr){
           ////////console.log(val);
           alltexts+=pData.property[index].property[val.i].name+',';
           let objs={
               oneid:val.oneid,
               childid: val.i,
               text_key: pData.property[index].name,
               text: pData.property[index].property[val.i].name
           };
           // ////////console.log('一种规格信息');
           property.push(objs);
           //
           //console.log('找到单个商品的价格');
           //console.log(pData.property[index].property[val.i].val);
           unitPrice=parseFloat( pData.property[index].property[val.i].val);
           //console.log(unitPrice);
          // yPrice:10
           // //////console.log(objs);
       })
    });

    unitPrice+=parseFloat(pData.jg);
    unitPrice=round2(unitPrice);
    let obj={
        allid: specif,
        alltext: alltexts,
        buyNum: 1,//这种规格的购物车数量
        price: setPrice,
        integral: pData.integral,
        property,
        unitPrice,
        errorFont: "",
        YNproperty: true,
        choose_ed_id: "0.2|0",
    };


    return obj;
}
//自定义保留的小数点
function round2(x) {

    var f_x = parseFloat(x);
    f_x = Math.round(f_x * 100) / 100;
    var s_x = f_x.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
    }
    return s_x;
}
//属性商品的操作 拿到类id和属性id以及购物车数据
function getPDataIndex(pIdY,pId,cartData){
    //暂时不考虑多店

    let pid=pId.split(":")[0]; //字符分割
    let obj={};
    if(cartData[0]){
        cartData[0].buydata.forEach(function(value,index,arr){
            ////////console.log(value);
            if(value.goodsid==pid){
                ////////console.log('找到类商品的位置');
                ////////console.log(index);
                obj.p=index;
                value.buy_property.forEach(function(val,ind,Arr){
                    if(val.allid==pId){
                        ////////console.log('找到属性商品的位置');
                        ////////console.log(ind);
                        obj.pArrt=ind;
                    }
                })
            }
        });
    }

    ////////console.log('查找完成');
    ////////console.log(obj);
    return obj;
}
//转json缓存
 function setwxage(setData,that) {
    ////console.log(that);
    var toStr = JSON.stringify(setData);
    wx.setStorage({
        key: "cartData",
        data: toStr,
        success: function () {
            ////console.log('检查2');
            ////console.log(setData);
            ////console.log(that);
            getNumPrice(setData,that);
            that.setData({
                cartIsHidden:false
            })
            //添加成功 弹窗提示
            wx.showToast({
                title: '成功添加购物车',
                duration: 800
            });
            //console.log('计算总价数量');
        }

    });


}
module.exports = {
    http: http,
    toast: toast,
    modal: modal,
    getDay: getDay,
    onCall: onCall,
    pay: pay,
    setNavigationBarTitle: setNavigationBarTitle,
    operating:operating,
    getNumPrice:getNumPrice,
    getAddress:getAddress,
    closeDebug:closeDebug,
    debug:debug,
    removeCart:removeCart,
    getCart:getCart,
    debug:debug,
    closeDebug:closeDebug,
    addCart:addCart,
    round2:round2,
}






















































